/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_CAN.h
 *
 *  Created on: 2022年1月11日
 *      Author: 64435
 */

#ifndef HARDWARE_LAYER_INC_LOS_CAN_H_
#define HARDWARE_LAYER_INC_LOS_CAN_H_

#include "HL_can.h"
#include "string.h"

#define LOS_CAN_NUM1 1U
#define LOS_CAN_NUM2 2U

/*  oID 有效位宏定义
* oID1 表示设备识别申请帧ID
* oID2 表示设备识别允许帧ID
*/
#define LOS_oID1_Storage 0x0153510C
#define LOS_oID2_Storage 0x0253510C

#define LOS_oID1_TTCBoard 0x0142130C
#define LOS_oID2_TTCBoard 0x0242130C

#define LOS_oID1_Baseband 0x0142140C
#define LOS_oID2_Baseband 0x0242140C

#define LOS_oID1_StarSensorY1 0x01DBB801
#define LOS_oID2_StarSensorY1 0x02DBB801

#define LOS_oID1_StarSensorZ1 0x01DBB802
#define LOS_oID2_StarSensorZ1 0x02DBB802

#define LOS_oID1_StarSensorY2 0x01DBB803
#define LOS_oID2_StarSensorY2 0x02DBB803

#define LOS_oID1_StarSensorZ2 0x01DBB804
#define LOS_oID2_StarSensorZ2 0x02DBB804

#define LOS_oID1_SunSensor 0x01DBB102
#define LOS_oID2_SunSensor 0x02DBB102

#define LOS_oID1_WheelX1 0x01FDD001
#define LOS_oID2_WheelX1 0x02FDD001

#define LOS_oID1_WheelY1 0x01FDD002
#define LOS_oID2_WheelY1 0x02FDD002

#define LOS_oID1_WheelZ1 0x01FDD004
#define LOS_oID2_WheelZ1 0x02FDD004

#define LOS_oID1_WheelS1 0x01FDD007
#define LOS_oID2_WheelS1 0x02FDD007

#define LOS_oID1_WheelX2 0x01FDD003
#define LOS_oID2_WheelX2 0x02FDD003

#define LOS_oID1_WheelY2 0x01FDD005
#define LOS_oID2_WheelY2 0x02FDD005

#define LOS_oID1_WheelZ2 0x01FDD006
#define LOS_oID2_WheelZ2 0x02FDD006

#define LOS_oID1_WheelS2 0x01FDD008
#define LOS_oID2_WheelS2 0x02FDD008

#define LOS_oID1_GNSS 0x01503901
#define LOS_oID2_GNSS 0x02503901

#define LOS_oID1_Ball 0x01512F01
#define LOS_oID2_Ball 0x02512F01

#define LOS_oID1_NaviEnhance 0x01282F01
#define LOS_oID2_NaviEnhance 0x02282F01

#define LOS_oID1_Spacetime 0x01512F02
#define LOS_oID2_Spacetime 0x02512F02

#define LOS_oID1_GP 0x01512F03
#define LOS_oID2_GP 0x02512F03

#define LOS_oID1_AIS 0x01072F01
#define LOS_oID2_AIS 0x02072F01

#define LOS_oID1_AtomOxygen 0x01072F02
#define LOS_oID2_AtomOxygen 0x02072F02

#define LOS_oID1_BMOBC 0x01072F03
#define LOS_oID2_BMOBC 0x02072F03

#define LOS_oID1_CMOS 0x01AAA001 
#define LOS_oID2_CMOS 0x02AAA001

/* CAN总线消息帧 */
typedef struct LOS_CANMSG_t
{
    uint32_t ID;
    uint8_t msg[8];
}LOS_CANMSG_t;

/* CAN首部内容 */
typedef struct LOS_SNP_t
{
    uint16_t SNP_len;
    uint8_t SNP_tran;
    uint8_t SNP_source;
    uint8_t SNP_dest;
    uint8_t SNP_rec;
}LOS_SNP_t;

LOS_CANMSG_t canrcv[8];

/*CAN总线设备nID定义*/
#define nID_OBC 0XF0                //星务计算机（本机）
#define nID_BaseBand 0x02           //数传基带板
#define nID_Storage 0x03            //存储板
#define nID_TTCBoard 0x01           //测控板
#define nID_StarSensorY 0xB5        //星敏Y
#define nID_StarSensorZ 0xB6        //星敏Z
#define nID_SunSensor 0xB7          //太敏
#define nID_WheelX 0xB1             //飞轮X
#define nID_WheelY 0xB2             //飞轮Y
#define nID_WheelZ 0xB3             //飞轮Z
#define nID_WheelS 0xB4             //飞轮S
#define nID_Gyro 0xB9               //光纤陀螺
#define nID_MEMS 0xBA               //MEMS陀螺
#define nID_GNSS 0xB8               //GNSS板
#define nID_Ball 0xA4               //增阻球
#define nID_NaviEnhance 0xA5        //导航增强
#define nID_Spacetime 0xA6          //时空表
#define nID_CMOS 0xA8               //CMOS小相机
#define nID_GP 0xA7                 //冷气推进
#define nID_AIS 0xA1                //AIS接收机
#define nID_AtomOxygen 0xA2         //原子氧探测器
#define nID_BMOBC 0xA3              //鲍曼OBC
#define nID_MagMe 0xBA              //磁强计
#define nID_PD 0X04                 //配电扩展板
#define nID_TC 0X05                 //热控扩展板
#define nID_CIOMPCam 0xA9           //长光高分相机
#define nID_FotonAmur 0xAF          //阿穆尔光电传感器
#define nID_AOCS 0xAA               //姿轨控分系统（用于遥测组帧）
#define nID_YH50 0XB9               //光纤陀螺
#define nID_MAGTRQX1 0X31           //磁力矩器X+
#define nID_MAGTRQY1 0X32           //磁力矩器Y+
#define nID_MAGTRQZ1 0X33           //磁力矩器Z+
#define nID_MAGTRQX2 0X34           //磁力矩器X-
#define nID_MAGTRQY2 0X35           //磁力矩器Y-
#define nID_MAGTRQZ2 0X36           //磁力矩器Z-

//动力学调试用
#define nID_MEMSDyn 0xCE
#define nID_MagMe_Dyn 0x33

/* CAN总线接收数据帧ID定义（19F0nIDXX） */
uint32_t CAN_DataID_BaseBand;
uint32_t CAN_DataID_Storage;
uint32_t CAN_DataID_TTCBoard;
uint32_t CAN_DataID_BaseBand;
uint32_t CAN_DataID_BaseBand;
uint32_t CAN_DataID_BaseBand;
uint32_t CAN_DataID_BaseBand;

/* CAN总线帧类型定义 */
#define FrameType_Radio 0U
#define FrameType_Apply 1U
#define FrameType_Permit 2U
#define FrameType_Data 10U
#define FrameType_File 11U
#define FrameType_Time 12U


/* CAN数据传输帧应答模式 */
#define ACK_Need 0    //需要应答
#define ACK_NoNeed 1  //不需要应答
#define ACK_Self 2    //应答帧本尊


void LOS_CAN_Init(void);
uint32_t LOS_CAN_Transmit(uint8_t CAN_NUM, uint32_t ID, uint8_t* data, size_t datalen);
uint32_t LOS_CAN_Receive(uint8_t CAN_NUM, LOS_CANMSG_t* canmsg, uint32_t canMESSAGE_BOX);
uint8_t LOS_CAN_nID(LOS_CANMSG_t* CAN_Frame_Apply, LOS_CANMSG_t* CAN_Frame_Permit);
void LOS_CAN_FirstPart(LOS_SNP_t* SNP, uint8_t* FirstPart);
uint32_t LOS_CAN_IDGen(uint8_t ACK_Type, uint8_t srcnID, uint8_t desnID, uint8_t order_num);
void LOS_CAN_Debug(LOS_CANMSG_t* canmsg);
void LOS_CAN_Radio();
void LOS_CAN_DoNothing();
void LOS_CAN_SendPack(uint8_t CANNUM, uint8_t desnID, uint8_t* pack, uint16_t len);

#endif /* HARDWARE_LAYER_INC_LOS_CAN_H_ */
